package com.wsleo.common.utils;

import java.awt.List;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

import com.wsleo.common.utils.test.Cat;

public class BeanUtils {

	public static Map<String, Object> describe(Object bean)
			throws IllegalAccessException, InvocationTargetException,
			NoSuchMethodException {
		return removeUnSuitableFields(bean);
	}

	@SuppressWarnings("unchecked")
	private static Map<String, Object> removeUnSuitableFields(Object bean)
			throws IllegalAccessException, InvocationTargetException,
			NoSuchMethodException {
		Map<String, Object> props = org.apache.commons.beanutils.BeanUtils.describe(bean);
//		props = removeClassKey(props);
//		props = removeCollectionKey(bean, props);

		return props;
	}

	private static Map<String, Object> removeClassKey(Map<String, Object> props) {
		if (props.containsKey("class")) {
			props.remove("class");
		}
		return props;
	}

	private static Map<String, Object> removeCollectionKey(Object bean, Map<String, Object> props) {
		Field[] fields = bean.getClass().getFields();
		for (Field f : fields) {
			isSuitable(f);
		}
		return props;
	}
	
	private static boolean isSuitable(Field field) {
		System.out.println(field.getName() + field.getClass().equals(List.class));
		return field.getClass().equals(List.class);
	}
	
	public static void main(String args[]) {
		try {
			BeanUtils.describe(new Cat());
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
	}
	
	public static boolean isCompatibleType(Object value, Class<?> type) {
	    if (type.isPrimitive()) {
	        return true;
	        
	    }  else if (type.isInstance(value)) {
            return true;
            
        } else if (type.isEnum()) {
            return true;
        } else {
            return isCompatibleType(type);
	    }
	}
	
	public static boolean isCompatibleType(Class<?> type) {
	    String normalType = "Integer,Longc,Double,Float,Short,Byte,Character,Boolean,String,Timestamp,Date";
	    int i = type.getName().lastIndexOf(".");
	    String name = type.getName().substring(i + 1);
	    return normalType.contains(name);
	}

}
